package com.example.photography.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.photography.R;
import com.example.photography.common.BaseActivity;
import com.example.photography.view.Message_Dialog;
import com.hyphenate.chat.EMClient;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Setting_Activity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.rl_account)
    RelativeLayout rlAccount;
    @BindView(R.id.rl_privacy)
    RelativeLayout rlPrivacy;
    @BindView(R.id.rl_message)
    RelativeLayout rlMessage;
    @BindView(R.id.rl_universal)
    RelativeLayout rlUniversal;
    @BindView(R.id.rl_help)
    RelativeLayout rlHelp;
    @BindView(R.id.rl_aboutus)
    RelativeLayout rlAboutus;
    @BindView(R.id.rl_feedback)
    RelativeLayout rlFeedback;
    @BindView(R.id.tv_quit)
    TextView tvQuit;
    private Message_Dialog mMyDialog;


    @Override
    protected void initData() {
        initListen();

    }

    private void initListen() {
        rlAboutus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToActivity(About_us.class, new Bundle());
            }
        });
        rlMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToActivity(Inform_Activity.class, new Bundle());
            }
        });

        tvQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View message_dialog = getLayoutInflater().inflate(R.layout.message_dialog, null);
                mMyDialog = new Message_Dialog(Setting_Activity.this, 0, 0, message_dialog, R.style.DialogTheme);
                mMyDialog.setCancelable(true);
                TextView tv_title = message_dialog.findViewById(R.id.tv_title);
                TextView tv_message = message_dialog.findViewById(R.id.tv_message);
                tv_title.setText("温馨提醒");
                tv_message.setText("退出当前账号后会清楚登录记录，确定退出？");
                Button btn_cancel = message_dialog.findViewById(R.id.btn_cancel);
                Button btn_confirm = message_dialog.findViewById(R.id.btn_confirm);
                btn_cancel.setText("取消");
                btn_confirm.setText("退出");
                mMyDialog.show();
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mMyDialog.dismiss();
                    }
                });
                btn_confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mMyDialog.dismiss();
                        //清除登录缓存数据
                        SharedPreferences sharedPreferences = getSharedPreferences("isLogin", MODE_PRIVATE);
                        sharedPreferences.edit().clear().apply();
                        goToActivity(Login_activity.class, new Bundle());
                        //退出环信
                        EMClient.getInstance().logout(true);
                        removeCurrentActivity();
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //防止窗体泄漏
        if (mMyDialog != null) {
            mMyDialog.dismiss();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
